/*
* detect_face_test.cpp
* client端人脸检测测试
*  Created on: 2018年05月13日
*      Author: guyadong
*/
#include <iostream>
#include <faceapiclient/FaceApiIfClient.h>
#include "BaseCmdParam.h"
#include <gfaux_tools/gfaux_tools.h>
#include "sample_log.h"

using namespace gdface;
using namespace net::gdface::sdk;
using namespace gdface::ui;
using namespace codegen;
namespace gdface {
	class CompareConfig : public BaseClientConfig {
	public:
		std::string leftimg, rightimg;
		bool show,rect;
		CompareConfig() {
			// --left 指定要比对的原人脸图像文件
			cmdparam.add<std::string>("left", 'l', "jpeg image path for left image", false,"images/idphoto1.jpg");
			// --right 指定要比对的目标人脸图像文件
			cmdparam.add<std::string>("right", 'r', "jpeg image path for right image", false, "images/idphoto2.jpg");

			// --show 指定是否以在图像上显示检测到人脸位置
			cmdparam.add<bool>("show", 0, "show compare result on image", false, true);
			cmdparam.add<bool>("rect", 0, "show face rectangle on image", false, true);
		}
	protected:
		void afterParse() {
			BaseClientConfig::afterParse();
			leftimg = cmdparam.get<std::string>("left");
			rightimg = cmdparam.get<std::string>("right");
			show = cmdparam.get<bool>("show");
			rect = cmdparam.get<bool>("rect");
		}
	};
}

int main(int argc, char *argv[]) {

	CompareConfig param;
	param.parse(argc, argv);

	auto client = FaceApiIfClient(param.host, param.port);

	try {
		SAMPLE_OUT("connect service:{}@{} ...", param.port, param.host);
		SAMPLE_OUT("LEFT IMAGE:{}",param.leftimg);
		SAMPLE_OUT("RIGHT IMAGE:{}", param.rightimg);
		std::vector<CodeInfo> codes;
		auto leftimgData = gdface::load_string(param.leftimg.c_str());
		auto rightimgData = gdface::load_string(param.rightimg.c_str());

		auto similarity = client.detectAndCompare2Face(leftimgData, FRect(), rightimgData, FRect());
		if (param.show) {
			auto title_prefix = "detectAndCompare2Face response ";
			if (param.rect) {
				auto leftpos = client.detectFace(leftimgData);
				auto rightpos = client.detectFace(rightimgData);
				showImage2In1(leftimgData, rightimgData, similarity, title_prefix, leftpos->at(0).__get_pos(), rightpos->at(0).__get_pos());
			}else{
				showImage2In1(leftimgData, rightimgData, similarity, title_prefix);
			}
		}
		else {
			SAMPLE_OUT("similarity={}", similarity);
		}
	}
	catch (std::exception& tx) {
		SAMPLE_ERR("ERROR: {}", tx.what());
	}
}